package vns.client.helper;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ListBox;

import vns.client.service.SupperAdminService;
import vns.client.service.SupperAdminServiceAsync;
import vns.shared.ObjectTypeDTO;

public class Helper {
	private static Helper singleton;
	public static List<ValueAndName> RULETYPE;
	public static List<ValueAndName> ACTIONS;
	public enum PARAMTYPE{
		RULE,
		COLUMN,
		FILLTER
	};
	public final static String SCOPE = "https://www.google.com/calendar/feeds/ https://www.google.com/m8/feeds/ https://docs.google.com/feeds/ https://mail.google.com/mail/feed/atom https://www.blogger.com/feeds/";
	private final static SupperAdminServiceAsync rpcSupperAdminServiceAsync = GWT
			.create(SupperAdminService.class);
	public static String uploadPathAction = null;

	public Helper() {
		List<ValueAndName> temp = new ArrayList<ValueAndName>();
		temp.clear();
		temp.add(new ValueAndName("priod", "Định kì"));
		temp.add(new ValueAndName("create", "Tạo mới"));
		temp.add(new ValueAndName("edit", "Chỉnh sửa"));
		temp.add(new ValueAndName("edit", "Tạo & Sửa"));
		temp.add(new ValueAndName("delete", "Xóa bỏ"));
		temp.add(new ValueAndName("view", "Xem"));
		RULETYPE = temp;
		
		temp.clear();
		temp.add(new ValueAndName("error", "Báo lỗi"));
		temp.add(new ValueAndName("warning", "Cảnh báo"));
		temp.add(new ValueAndName("disable", "Tắt thuộc tính"));
		ACTIONS = temp;
		
		singleton = this;
	}

	public static void getUploadPathAction() {
		new RPCCall<String>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Không lấy được upload path!");
			}

			@Override
			public void onSuccess(String result) {
				uploadPathAction = result;
			}

			@Override
			protected void callService(AsyncCallback<String> cb) {
				rpcSupperAdminServiceAsync.test(cb);
			}

		}.retry(3);
	}

	/*
	 * Swap between row and index row = index + 1 index = row - 1 row > 1 index
	 * > 0
	 */
	public static int getIndexByRow(int row, int listSize) {
		int index = row - 1;
		if (index < 0) {
			return -1;
		} else if (index > listSize - 1) {
			return -1;
		} else {
			return index;
		}
	}

	public static int getRowByIndex(int index, int flextableRow) {
		int row = index + 1;
		if (row < 1) {
			return -1;
		} else if (row > flextableRow - 1) {
			return -1;
		} else {
			return row;
		}
	}

	public static Helper get() {
		return singleton;
	}

	public ListBox getListBox(List<ValueAndName> list, List<String> listValues) {
		ListBox lb = new ListBox(true);
		if (list != null) {
			if (listValues == null)
				listValues = new ArrayList<String>();
			int index = 0;
			for (ValueAndName van : list) {
				lb.addItem(van.getName(), van.getValue());
				for (String s : listValues) {
					if (s.equalsIgnoreCase(van.getValue())) {
						lb.setItemSelected(index, true);
					}
				}
				index++;
			}
		}
		return lb;
	}

	public ListBox getListBox(List<ValueAndName> list, String value) {
		ListBox lb = new ListBox(false);
		if (list != null) {
			if (value == null)
				value = "";
			int index = 0;
			for (ValueAndName van : list) {
				lb.addItem(van.getName(), van.getValue());
				if (value.equalsIgnoreCase(van.getValue())) {
					lb.setItemSelected(index, true);
				}
				index++;
			}
		}
		return lb;
	}

	public List<ValueAndName> getListValueAndName(List<ObjectTypeDTO> list) {
		List<ValueAndName> listVAN = new ArrayList<ValueAndName>();
		for (ObjectTypeDTO otDTO : list) {
			ValueAndName van = new ValueAndName(KeyFactory.keyToString(otDTO
					.getId()), otDTO.getName());
			listVAN.add(van);
		}
		return listVAN;
	}
}
